function modify_info(res) {
    const name = document.getElementById("name");
    const phone = document.getElementById("phone");
    const photo = document.getElementById("photo");
    const i_name = document.getElementById("i-name");
    const i_code = document.getElementById("i-code");
    const i_type = document.getElementById("i-type");
    const i_sex = document.getElementById("i-sex");
    const i_college = document.getElementById("i-college");
    const i_square = document.getElementById("i-square");

    i_name.innerText = name.innerText = res["name"];
    phone.innerText = res["telephone"];
    photo.style.setProperty("background-image", "url('" + res["photo"] + "')");
    document.styleSheets[1].insertRule(".personal-pic:after{background-image: url(\"" + res["photo"] + "\");}", document.styleSheets[1].cssRules.length);
    i_code.innerText = res["student_code"];
    i_type.innerText = res["student_type"];
    i_sex.innerText = res["sex"];
    i_college.innerText = res["college"];
    i_square.innerText = res["square"];
}


function modify_leave(res) {
    const name = document.getElementById("l-name");
    const college = document.getElementById("l-college");
    const square = document.getElementById("l-square");
    const sex = document.getElementById("l-sex");
    const code = document.getElementById("l-code");
    const phone = document.getElementById("l-phone");
    const confirm = document.getElementById("leave-confirm");

    name.value = res["name"];
    college.value = res["college"];
    square.value = res["square"];
    sex.value = res["sex"];
    code.value = res["student_code"];
    phone.value = res["telephone"];

    confirm.onclick = () => {
        let reason;
        const area = document.getElementById("l-area");
        const time_start = document.getElementById("l-time-start");
        const time_end = document.getElementById("l-time-end");
        const reason_radios = document.getElementsByName("reason-radio");
        const reason_text = document.getElementById("reason-text");

        if (isNull(area.value) || isNull(time_start.value) || isNull(time_end.value) || isNull(reason_text.value)) {
            alert("请填写完整信息！");
            return;
        }

        const ts = new Date(time_start.value);
        const te = new Date(time_end.value);
        const tn = new Date();

        if (te - ts <= 0 || ts - tn < 0) {
            alert("外出时间填写错误！");
            return;
        }


        for (let i = 0; i < reason_radios.length; i++) {
            if (reason_radios[i].checked) {
                reason = reason_radios[i].value;
            }
        }

        const data = {
            "token": sessionStorage.getItem("token"),
            "leavingDestination": area.value,
            "leavingStart": time_start.value,
            "leavingEnd": time_end.value,
            "leavingReason": reason,
            "leavingDetail": reason_text.value
        };

        httpRequest("http://172.27.71.125:7402/leave/api/askforleave", JSON.stringify(data),
            _ => {
                alert("提交成功！");
                area.value = "";
                time_start.value = "";
                time_end.value = "";
                reason_text.value = "";
                reason_radios[0].checked = false;
                fresh();
            },
            _ => {
                alert("未知错误");
            });
    };
}

function modify_history(token) {
    httpRequest("http://172.27.71.125:7402/leave/api/getleavings", JSON.stringify({"token": token}),
        response => {
            const res = JSON.parse(response)["result"];
            const tbody = document.getElementById("history-table").children[1];
            const t_length = tbody.rows.length;
            for (let i = 0; i < t_length; i++) {
                tbody.deleteRow(0);
            }
            for (let i = 0; i < res.length; i++) {
                tbody.insertRow(i);
                for (let j = 0; j < 4; j++) {
                    tbody.rows[i].insertCell(j);
                    tbody.rows[i].cells[j].style = "vertical-align: middle;";
                }
                tbody.rows[i].cells[0].innerText = res[i]["leaving_date"].replaceAll("-", ".") + "-" + res[i]["leaving_end"].replaceAll("-", ".");
                tbody.rows[i].cells[1].innerText = res[i]["leaving_reason"];
                tbody.rows[i].cells[2].innerText = res[i]["leaving_state"];
                tbody.rows[i].cells[3].innerHTML = "<a class=\"btn btn-outline-primary\" href=\"detail.html?leaving_id=" + res[i]["leaving_id"] + "&token=" + sessionStorage.getItem("token") + "&identity=" + sessionStorage.getItem("identity") + "\" target=\"_blank\">查看详情</a>";
            }
        },
        _ => {
            urlReplace("login.html");
        });
}


function fresh() {
    const token = sessionStorage.getItem("token");
    const t_logout = document.getElementById("logout");

    t_logout.onclick = logout;

    if (token == null || token === "") {
        urlReplace("login.html");
        return;
    }


    httpRequest("http://172.27.71.125:7402/leave/api/getstudent", JSON.stringify({"token": token}),
        response => {
            const res = JSON.parse(response)["result"];
            modify_info(res);
            modify_leave(res);
            modify_history(token);
        },
        _ => {
            urlReplace("login.html");
        });
}

window.onfocus = fresh;
window.onload = fresh;
